<template>
  <div>
    <!-- 开始使用 -->
    <el-dialog title="开始使用" :visible.sync="startVisible">
      <div style="margin:30px 50px">
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">业务编号</label>
            <div class="layui-input-inline">
              <el-input v-model="regbusnum" placeholder="业务编号" v-focus @keyup.enter.native="getRegInfo($event)" clearable></el-input>  
            </div>
          </div>
          <!-- <div class="layui-inline">
            <div class="layui-input-inline">
              <el-button type="primary" @click="openRegCheck" >选取逝者</el-button>
            </div>
          </div> -->
        </div>
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">逝者姓名</label>
            <div class="layui-input-inline">
              <el-input v-model="regData.REGNAME" readonly></el-input>
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">死亡日期</label>
            <div class="layui-input-inline">
              <el-input v-model="regData.REGDEATHDATE" readonly></el-input>
            </div>
          </div>
        </div>
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">经办家属</label>
            <div class="layui-input-inline">
              <el-input v-model="regData.REGHANDLERNAME" readonly></el-input>
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">联系电话</label>
            <div class="layui-input-inline">
              <el-input v-model="regData.REGHANDLERLINK" readonly></el-input>
            </div>
          </div>
        </div>
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">开始日期</label>
            <div class="layui-input-inline">
              <el-date-picker v-date v-model="startDate" type="date" class="longtext" placeholder="选择日期时间"> </el-date-picker>
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">结束日期</label>
            <div class="layui-input-inline">
              <el-date-picker v-date v-model="endDate" type="date" class="longtext" placeholder="选择日期时间"> </el-date-picker>
            </div>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer" style="text-align:center;">
        <el-button type="primary" :loading="this.$store.state.isloading" @click="dataStart" :disabled="IsSubmit">
          <i class="layui-icon">&#xe62f;</i><span> 开始使用</span>
        </el-button>
      </div>
    </el-dialog>
    <!-- 结束使用 -->
    <el-dialog title="结束使用" :visible.sync="endVisible">
      <div style="margin:30px 50px">
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">逝者姓名</label>
            <div class="layui-input-inline">
              <el-input v-model="regData.REGNAME" readonly></el-input>
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">死亡日期</label>
            <div class="layui-input-inline">
              <el-input v-model="regData.REGDEATHDATE" readonly></el-input>
            </div>
          </div>
        </div>
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">经办家属</label>
            <div class="layui-input-inline">
              <el-input v-model="regData.REGHANDLERNAME" readonly></el-input>
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">联系电话</label>
            <div class="layui-input-inline">
              <el-input v-model="regData.REGHANDLERLINK" readonly></el-input>
            </div>
          </div>
        </div>
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">开始日期</label>
            <div class="layui-input-inline">
              <el-input v-model="startDateStr" readonly></el-input>
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">结束日期</label>
            <div class="layui-input-inline">
              <el-input v-model="endDateStr" readonly></el-input>
            </div>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer" style="text-align:center;">
        <el-button type="primary" :loading="this.$store.state.isloading" @click="dataEnd">
          <i class="layui-icon">&#xe601;</i><span> 结束使用</span>
        </el-button>
      </div>
    </el-dialog>
    <!-- 使用续存 -->
    <el-dialog title="使用续存" :visible.sync="conVisible">
      <div style="margin:30px 50px">
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">逝者姓名</label>
            <div class="layui-input-inline">
              <el-input v-model="regData.REGNAME" readonly></el-input>
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">死亡日期</label>
            <div class="layui-input-inline">
              <el-input v-model="regData.REGDEATHDATE" readonly></el-input>
            </div>
          </div>
        </div>
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">经办家属</label>
            <div class="layui-input-inline">
              <el-input v-model="regData.REGHANDLERNAME" readonly></el-input>
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">联系电话</label>
            <div class="layui-input-inline">
              <el-input v-model="regData.REGHANDLERLINK" readonly></el-input>
            </div>
          </div>
        </div>
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">开始日期</label>
            <div class="layui-input-inline">
              <el-date-picker v-date v-model="startDate" type="date" class="longtext" placeholder="选择日期时间"> </el-date-picker>
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">结束日期</label>
            <div class="layui-input-inline">
              <el-date-picker v-date v-model="endDate" type="date" class="longtext" placeholder="选择日期时间"> </el-date-picker>
            </div>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer" style="text-align:center;">
        <el-button type="primary" :loading="this.$store.state.isloading" @click="dataCon">
          <i class="layui-icon">&#xe61f;</i><span> 使用续存</span>
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style  rel="stylesheet/scss" lang="scss" scoped>

</style>
<script>
import apiBase from "@/api/apiBase.js";
import { formatDate, timeStringToDate } from "@/utils/date.js";
export default {
  data() {
    return {
      OPEARTCODE: {
        KSSY: "开始使用",
        JSSY: "结束使用",
        SYXC: "使用续存"
      },
      startVisible: false,
      endVisible: false,
      conVisible: false,
      regbusnum: null,
      startDate: null,
      endDate: null,
      startDateStr: "",
      endDateStr: "",
      regData: {},
      ashData: {},
      ass: {},
      IsSubmit: true
    };
  },
  methods: {
    openRegCheck() {},
    open(ass, order) {
      Object.assign(this.$data, this.$options.data());
      this.ass = ass;
      switch (order) {
        case this.OPEARTCODE.KSSY:
          this.startDate = new Date();
          var now = new Date();
          this.endDate = now.setFullYear(now.getFullYear() + 1);
          this.startVisible = true;
          break;
        case this.OPEARTCODE.JSSY:
          this.getRegByNO(setDate => {
            this.startDateStr = this.ashData.ASHBEGDATE;
            this.endDateStr = this.ashData.ASHENDDATE;
            var end = new Date(this.endDateStr);
            if (end > new Date()) {
              this.endDateStr = formatDate(new Date(), "yyyy-MM-dd");
            }
          });
          this.endVisible = true;
          break;
        case this.OPEARTCODE.SYXC:
          this.getRegByNO(setDate => {
            var end = new Date(this.ashData.ASHENDDATE);
            var newStart = new Date(end.setDate(end.getDate() + 1));
            this.startDate = end;
            this.endDate = newStart.setFullYear(newStart.getFullYear() + 1);
          });
          this.conVisible = true;
          break;
      }
    },
    //根据正式业务编号获取ID
    getRegInfo() {
      let data = {
        REGNO: this.regbusnum
      };
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Registration.GetRegistrationByNo,
        res => {
          var reg = res.data.content;
          if (reg) {
            this.regData = res.data.content;
            this.IsSubmit = false;
          }
          console.log("reg", res.data.content);
        }
      );
    },
    //根据冰柜号和业务编号获取
    getRegByNO(setDate) {
      if (this.ass.AssUse && this.ass.AssUse.length > 0) {
        let data = {
          REGBUSNUM: this.ass.AssUse[0].REGBUSNUM,
          ASHASSID: this.ass.ASSID
        };
        apiBase.DoService(
          data,
          apiBase.getApiAdress().AshesUse.GetRegistrationAndUseByNo,
          res => {
            console.log(res.data.content);
            var data = res.data.content;
            this.ashData = data.ASH;
            this.regData = data.REG;
            setDate();
          }
        );
      }
    },
    //开始使用
    dataStart() {
      let Data = {
        ASHASSID: this.ass.ASSID,
        ASHREGID: this.regData.REGID,
        ASHBEGDATE: formatDate(this.startDate, "yyyyMMdd"),
        ASHENDDATE: formatDate(this.endDate, "yyyyMMdd")
      };
      apiBase.DoService(Data, apiBase.getApiAdress().AshesUse.StartUse, res => {
        //this.refreshData();
        //this.$refs.dialog.close();
        this.startVisible = false;
        this.$message({
          message: "开始使用成功",
          type: "success"
        });
        this.$emit("refresh");
      });
    },
    //结束使用
    dataEnd() {
      let Data = {
        ASHASSID: this.ass.ASSID,
        ASHREGID: this.regData.REGID,
        ASHTAKDATE: formatDate(this.endDateStr, "yyyyMMdd"),
        ASHID: this.ashData.ASHID
      };
      apiBase.DoService(Data, apiBase.getApiAdress().AshesUse.EndUse, res => {
        this.endVisible = false;
        this.$message({
          message: "结束使用成功",
          type: "success"
        });
        this.$emit("refresh");
      });
    },
    //使用续存
    dataCon() {
      let data = {
        ASHID: this.ashData.ASHID,
        ASHREGID: this.regData.REGID,
        ASHBEGDATE: formatDate(this.startDate, "yyyyMMdd"),
        ASHENDDATE: formatDate(this.endDate, "yyyyMMdd"),
        ASHASSID: this.ass.ASSID
      };
      apiBase.DoService(data, apiBase.getApiAdress().AshesUse.ConUse, res => {
        this.conVisible = false;
        this.$message({
          message: "使用续存成功",
          type: "success"
        });
        this.$emit("refresh");
      });
    }
  },
  directives: {
    focus: {
      update: function(el) {
        el.children[0].focus();
      }
    }
  }
};
</script>

